package com.alpha.eceasy.mall.api.constant;

import com.alpha.eceasy.exception.BizException;
import com.alpha.eceasy.exception.ErrorCodeEnum;
import com.baomidou.mybatisplus.annotation.EnumValue;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Optional;

/**
 * 配送方式定义
 */
@Getter
@AllArgsConstructor
public enum FreightEnum {
    MALL_COURIER_FREIGHT(0, "商城快递配送", BigDecimal.valueOf(0.01)),
    MALL_FLASH_SEND_FREIGHT(1, "商城同城配送", BigDecimal.valueOf(0.01)),
    INVITE_FREIGHT(2, "到店自取", BigDecimal.ZERO),
    REWARD_FREIGHT(3, "奖励配送", BigDecimal.valueOf(9)),
    PACKAGE_MAIL(4, "包邮", BigDecimal.ZERO);

    @EnumValue
    private final Integer code;
    private final String name;
    @Setter
    private BigDecimal price;

    public static FreightEnum valueFromCode(Integer code) {
        Optional<FreightEnum> first = Arrays.stream(values()).filter(item -> item.getCode().equals(code)).findFirst();
        return first.orElseThrow(BizException::new);
    }

    public static FreightEnum valueFromCode(Integer code, String province) {
        String containsProvinceFirst = "山东省、江苏省、浙江省、上海市、安徽省、北京市、天津市、河南省、河北省、湖北省";
        String containsProvinceSecond = "重庆市、湖南省、广东省、福建省、江西省、山西省、陕西省、四川省、贵州省、广西省、黑龙江省、吉林省、辽宁省";
        String containsProvinceThird = "甘肃省、青海省、云南省、海南省、宁夏回族自治区、内蒙古自治区、新疆维吾尔自治区、西藏自治区";
        for (FreightEnum paymentMethod : values()) {
            if (paymentMethod.getCode().equals(code)) {
                if (containsProvinceFirst.contains(province)) {
                    paymentMethod.setPrice(BigDecimal.valueOf(0.01));
                } else if (containsProvinceSecond.contains(province)) {
                    paymentMethod.setPrice(BigDecimal.valueOf(8));
                } else if (containsProvinceThird.contains(province)) {
                    paymentMethod.setPrice(BigDecimal.valueOf(20));
                } else {
                    throw new BizException(ErrorCodeEnum.BAD_REQUEST);
                }
                return paymentMethod;
            }
        }
        throw new BizException();
    }
}
